'use strict';
(function() {
	
	function initParams($scope, $state, Dialog, service){
		$scope.servers = null;
		$scope.newServer = {"ip":"", "port":""};
		
		$scope.showMonitors = function(server){
			$state.go("main.app.monitor", {ip:server.ip, port:server.port});
		};
		
		$scope.delServer = function($event, server){
			Dialog.confirm("此操作将同时删除该服务器下所有监控对象的监控日志信息，是否确认删除？", function(){
				service.del(server.ip, server.port, function(rst){
					init($scope, $state, Dialog, service);
				});
			});
			$event.stopPropagation();
		};
		
		$scope.addServer = function(){
			if($scope.newServer.ip && $scope.newServer.port){
				for(var i = 0; i < $scope.servers.length; i++){
					var server = $scope.servers[i];
					if(server.ip == $scope.newServer.ip && server.port == $scope.newServer.port){
						Dialog.show("服务器已存在！", 300);
						return;
					}
				}
				service.create($scope.newServer.ip, $scope.newServer.port, function(rst){
					init($scope, $state, Dialog, service);
				});
			}else {
				Dialog.show("信息不完整，请完善！", 300);
			}
		};
	}
	
	function init($scope, $state, Dialog, service){
		service.list(function(rst){
			$scope.servers = rst.data;
		});
	}
	
    ngApp.controller("appserverController", ["$scope", "$state", "Dialog", 'AppConfigConstant', 'appserverService', function($scope, $state, Dialog, AppConfigConstant, appserverService) {
    	initParams($scope, $state, Dialog, appserverService);
    	init($scope, $state, Dialog, appserverService);
    }]);

})();
